#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstdio>
#include <cstring>
#include <errno.h>
void Setfcntl(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if (fl < 0)
    {
        perror("fcntl fail");
    }
    fcntl(fd, F_SETFL, fl | O_NONBLOCK);
    std::cout << " set " << fd << " nonblock done" << std::endl;
}
int main()
{
    Setfcntl(0);
    char buffer[1024];
    while (true)
    {

        printf("please Enter#");
        fflush(stdout);

        ssize_t n = read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << "echo: " << buffer << std::endl;
        }
        else if (n == 0)
        {
            std::cerr << "read done" << std::endl;
        }
        else
        {
            if (errno == EWOULDBLOCK)
            {
                std::cout << "try read" << std::endl;
                sleep(1);
            }
            else
            {
                std::cout << "read stop,errno:" << errno << "strerror:" << strerror(errno) << std::endl;
            }
        }
    }

    return 0;
}